{-  Copyright (C) 2014 Mark W Pearse  
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
-}

{-# LANGUAGE ForeignFunctionInterface #-}

module E_rawAPI where

import E_Data
import Foreign.Ptr
import Foreign.C (CString)
import Foreign.C.Types

-- Import the c functions
-- Platform Configuration
foreign import ccall "epiphany-hal-api.h e_init"
     e_init :: CString -> IO Int
foreign import ccall "epiphany-hal-api.h e_finalize"
     e_finalize :: IO Int
foreign import ccall "epiphany-hal-api.h e_get_platform_info"
     e_get_platform_info :: Ptr PlatformInfo -> IO Int

-- Epiphany access
foreign import ccall "epiphany-hal-api.h e_open"               -- defines an eCore workgroup
     e_open :: GroupRef -> Int -> Int -> Int -> Int -> IO Int  
--             group       row    col    rows   cols   error_flag
foreign import ccall "epiphany-hal-api.h e_close"              -- closes an eCore workgroup
     e_close :: GroupRef -> IO Int
foreign import ccall "epiphany-hal-api.h e_read"
     e_group_read :: GroupRef -> Int -> Int -> Int -> Ptr CChar -> Int -> IO Int 
--                   group       row    col    offset to-addr      size   number_read
foreign import ccall "epiphany-hal-api.h e_write"
     e_group_write :: GroupRef -> Int -> Int -> Int -> Ptr CChar -> Int -> IO Int 
--                    group       row    col    offset from-addr    size   number_written

-- External memory access
foreign import ccall "epiphany-hal-api.h e_alloc"              -- defines a buffer in external memory
     e_alloc :: Ptr Emem -> Int -> Int -> IO Int               
--              memref      offset size   error_flag
foreign import ccall "epiphany-hal-api.h e_free"
     e_free :: Ptr Emem -> IO Int
foreign import ccall "epiphany-hal-api.h e_read"
     e_mem_read :: Ptr Emem -> Int -> Int -> Int -> Ptr CChar -> Int -> IO Int 
--                     mem     na     na     offset dstptr       size   number_read
foreign import ccall "epiphany-hal-api.h e_write"
     e_mem_write :: Ptr Emem -> Int -> Int -> Int -> Ptr CChar -> Int -> IO Int 
--                      mem     na     na     offset srcptr       size   number_written

-- System control functions
foreign import ccall "epiphany-hal-api.h e_reset_system"
     e_reset_system :: IO Int
foreign import ccall "epiphany-hal-api.h e_reset_chip"
     e_reset_chip :: IO Int
foreign import ccall "epiphany-hal-api.h e_reset_group"
     e_reset_group :: GroupRef -> IO Int
foreign import ccall "epiphany-hal-api.h e_start"
     e_start :: GroupRef -> Int -> Int -> IO Int 
foreign import ccall "epiphany-hal-api.h e_start_group"
     e_start_group :: GroupRef -> IO Int 
foreign import ccall "epiphany-hal-api.h e_signal"
     e_signal :: GroupRef -> Int -> Int -> IO Int 
foreign import ccall "epiphany-hal-api.h e_halt"
     e_halt :: GroupRef -> Int -> Int -> IO Int 
foreign import ccall "epiphany-hal-api.h e_resume"
     e_resume :: GroupRef -> Int -> Int -> IO Int 

-- Utility functions
foreign import ccall "epiphany-hal-api.h e_get_num_from_coords"
     e_get_num_from_coords :: GroupRef -> Int -> Int -> Int
--                            group       row    col    corenum
foreign import ccall "epiphany-hal-api.h e_get_coords_from_num"
     e_get_coords_from_num :: GroupRef -> Int -> Ptr Int -> Ptr Int
--                            group       corenum    row        col    
foreign import ccall "epiphany-hal-api.h e_is_addr_on_chip"
     e_is_addr_on_chip :: Ptr () -> E_Bool
--                        addr      on-chip?   
foreign import ccall "epiphany-hal-api.h e_is_addr_on_group"
     e_is_addr_on_group :: GroupRef -> Ptr ()-> E_Bool
--                         group       addr     on-chip?     
foreign import ccall "epiphany-hal-api.h e_set_host_verbosity"
     e_set_host_verbosity :: Int -> IO Int -- E_Hal_Diag, 0-4
foreign import ccall "epiphany-hal-api.h e_set_host_verbosity"
     e_set_loader_verbosity :: Int -> IO Int -- E_Loader_Diag, 0-4

-- Loader
foreign import ccall "epiphany-hal-api.h e_load"
     e_load :: CString -> GroupRef -> Int -> Int -> E_Bool -> IO Int 
--             srec_file  group       row    col    start     error_flag
foreign import ccall "epiphany-hal-api.h e_load_group"
     e_load_group :: CString -> GroupRef -> Int -> Int -> Int -> Int -> E_Bool -> IO Int 
--                   srec_file  group       row    col    rows   cols   start     error_flag


foreign import ccall "unistd.h usleep"
    usleep :: Int -> IO ()


